package com.hw.paltfrom.service.weight;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.exception.HWSystemException;
import com.hw.paltfrom.mapper.HwDataOverrunstationHistoryMapper;
import com.hw.paltfrom.mapper.HwSupervisionMapper;
import com.hw.paltfrom.pack.ResponseResult;
import com.hw.paltfrom.pojo.HwBaseWorkflow;
import com.hw.paltfrom.pojo.HwCommandWorkflow;
import com.hw.paltfrom.pojo.HwDataOverrunstationHistory;
import com.hw.paltfrom.pojo.HwSupervision;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.service.doncutor.BaseWorkflowService;
import com.hw.paltfrom.service.doncutor.CommandWorkflowService;
import com.hw.paltfrom.utils.MapRemoveNullUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.DataOverrunstationHistoryVo;

/**
 * 超限检测站 邢家华 2019-11-6 16:13:03
 * 
 * @author Administrator
 *
 */
@Service
public class DataOverrunstationHistoryService {

	@Autowired
	private HwDataOverrunstationHistoryMapper hwDataOverrunstationHistoryMapper;

	@Autowired
	HwSupervisionMapper hwSupervisionMapper;

	@Autowired
	private BaseWorkflowService baseWorkflowService;

	@Autowired
	private CommandWorkflowService commandWorkflowService;

	// 全局查询条件+分页
	public Map<String, Object> queryAllList(HashMap<String, Object> vo) {

		Timestamp dStartTime = null;
		Timestamp dEndTime = null;
		Timestamp jStartTime = null;
		Timestamp jEndTime = null;
		int dflag = 0;
		int jflag = 0;

		// 动态称时间范围
		if (vo.get("dStartTime") == null) {
			if (vo.get("dEndTime") == null) {
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		} else {
			if (vo.get("dEndTime") != null) {
				dflag = 1;
				dStartTime = Timestamp.valueOf((String) vo.get("dStartTime"));
				dEndTime = Timestamp.valueOf((String) vo.get("dEndTime"));
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		}

		// 静态称时间范围
		if (vo.get("jStartTime") == null) {
			if (vo.get("jEndTime") == null) {
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		} else {
			if (vo.get("jEndTime") != null) {
				jflag = 1;
				jStartTime = Timestamp.valueOf((String) vo.get("jStartTime"));
				jEndTime = Timestamp.valueOf((String) vo.get("jEndTime"));
			} else {
				return ResponseResult.fail(HWSystemCode.PARAMS_ERROR);
			}
		}

		int pageNum = Integer.parseInt(vo.get("pageNum").toString());
		int pageSize = Integer.parseInt(vo.get("pageSize").toString());
		// 车牌号
		String cph = null;
		if (vo.get("cph") != null) {
			cph = (String) vo.get("cph");
		}
		// 超限率
		String cxl = null;
		if (vo.get("D_CXL") != null && !"".equals(vo.get("D_CXL"))) {
			cxl = vo.get("D_CXL").toString();
		}
		// 轴数
		String zs = null;
		if (vo.get("D_ZS") != null && !"".equals(vo.get("D_ZS"))) {
			zs = vo.get("D_ZS").toString();
		}
		// 超限吨数
		String cxds = null;
		if (vo.get("J_CXDS") != null && !"".equals(vo.get("J_CXDS"))) {
			cxds = vo.get("J_CXDS").toString();
		}
		String symbol = null;
		if (vo.get("symbol") != null && !"".equals(vo.get("symbol"))) {
			symbol = vo.get("symbol").toString();
		}

		String symbol1 = null;
		if (vo.get("symbol1") != null && !"".equals(vo.get("symbol1"))) {
			symbol1 = vo.get("symbol1").toString();
		}

		String symbol2 = null;
		if (vo.get("symbol2") != null && !"".equals(vo.get("symbol2"))) {
			symbol2 = vo.get("symbol2").toString();
		}

		// 行政区域编码
		String areaId = null;
		if (vo.get("F_AreaCode") != null && !"".equals(vo.get("F_AreaCode"))) {
			areaId = vo.get("F_AreaCode").toString();
		}

		// 超限站编码
		String overrunCode = null;
		if (vo.get("F_OverrunCode") != null && !"".equals(vo.get("F_OverrunCode"))) {
			overrunCode = vo.get("F_OverrunCode").toString();
		}

		// 分页处理
		PageHelper.startPage(pageNum, pageSize);
		// 去除模糊和分页字段
		vo.remove("D_CXL");
		vo.remove("D_ZS");
		vo.remove("J_CXDS");
		vo.remove("symbol");
		vo.remove("symbol1");
		vo.remove("symbol2");
		vo.remove("pageNum");
		vo.remove("pageSize");
		vo.remove("dStartTime");
		vo.remove("dEndTime");
		vo.remove("jStartTime");
		vo.remove("jEndTime");
		vo.remove("cph");
		vo.remove("F_AreaCode");
		vo.remove("F_OverrunCode");
		// 去除传值为空的字段
		MapRemoveNullUtil.removeNullEntry(vo);

		Wrapper<HwDataOverrunstationHistory> wrapper = new EntityWrapper<>();
		// 为空不是市或县，是站点 前端沟通好 市 F_AreaCode不传 县 传F_AreaCode， 站点传F_OverrunName
		// 不传F_AreaCode
		wrapper.allEq(vo);
		if (dflag == 1) {
			wrapper.and().between("D_JCSJ", dStartTime, dEndTime);
		}
		if (jflag == 1) {
			wrapper.and().between("J_JCSJ", jStartTime, jEndTime);
		}
		if (cph != null) {
			wrapper.like("CPH", cph);
		}
		if (StringUtils.isNotBlank(symbol) && StringUtils.isNotBlank(cxl)) {
			switch (symbol) {
			case "0":
				wrapper.eq("D_CXL", cxl);
				break;

			case "1":
				wrapper.gt("D_CXL", cxl);
				break;
			case "2":
				wrapper.lt("D_CXL", cxl);
				break;
			case "3":
				wrapper.ge("D_CXL", cxl);
				break;
			case "4":
				wrapper.le("D_CXL", cxl);
				break;
			}
		}
		if (StringUtils.isNotBlank(symbol1) && StringUtils.isNotBlank(zs)) {
			switch (symbol1) {
			case "0":
				wrapper.eq("D_ZS", zs);
				break;

			case "1":
				wrapper.gt("D_ZS", zs);
				break;
			case "2":
				wrapper.lt("D_ZS", zs);
				break;
			case "3":
				wrapper.ge("D_ZS", zs);
				break;
			case "4":
				wrapper.le("D_ZS", zs);
				break;
			}

		}
		if (StringUtils.isNotBlank(symbol2) && StringUtils.isNotBlank(cxds)) {
			switch (symbol2) {
			case "0":
				wrapper.eq("J_CXDS", cxds);
				break;
			case "1":
				wrapper.gt("J_CXDS", cxds);
				break;
			case "2":
				wrapper.lt("J_CXDS", cxds);
				break;
			case "3":
				wrapper.ge("J_CXDS", cxds);
				break;
			case "4":
				wrapper.le("J_CXDS", cxds);
				break;
			}
		}
		if (areaId != null) {
			wrapper.eq("F_AreaCode", areaId);
		}
		if (overrunCode != null) {
			wrapper.eq("F_OverrunCode", overrunCode);
		}

		wrapper.orderBy("D_JCSJ", false);
		List<HwDataOverrunstationHistory> hwDataOverrunstationHistoryList = hwDataOverrunstationHistoryMapper
				.selectList(wrapper);
		PageInfo<HwDataOverrunstationHistory> info = new PageInfo<>(hwDataOverrunstationHistoryList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
	}

	// 新增
	public Map<String, Object> insert(DataOverrunstationHistoryVo vo) {
		String fId = vo.getfId();
		String cph = vo.getCph();

		// 动态称重数据
		String dCpys = vo.getdCpys();
		Double dZz = vo.getdZz();
		Integer dXz = vo.getdXz();
		Integer dZs = vo.getdZs();
		Integer dCx = vo.getdCx();
		Double dCxl = vo.getdCxl();

		Integer dCd = vo.getdCd();
		Double dCs = vo.getdCs();
		String dFx = vo.getdFx();
		Integer dSfcx = vo.getdSfcx();
		String dCptx = vo.getdCptx();
		String dCttx = vo.getdCttx();

		// 静态称重数据
		Double jZz = vo.getjZz();
		Integer jSfcz = vo.getjSfcz();
		Double jCxds = vo.getjCxds();
		String jCzy = vo.getjCzy();
		String jCtpic = vo.getjCtpic();
		String jCspic = vo.getjCspic();
		String jCwpic = vo.getjCwpic();
		String jCppic = vo.getjCppic();

		String fAreacode = vo.getfAreacode();
		String fOverruncode = vo.getfOverruncode();
		String fOverrunname = vo.getfOverrunname();

		HwDataOverrunstationHistory hwDataOverrunstationVo = new HwDataOverrunstationHistory();
		hwDataOverrunstationVo.setfId(fId);
		hwDataOverrunstationVo.setCph(cph);
		if (fId != null) {
			// 静态称重数据

			Wrapper<HwDataOverrunstationHistory> wrapper = new EntityWrapper<HwDataOverrunstationHistory>();
			wrapper.eq("F_Id", fId);

			HwDataOverrunstationHistory hwDataOverrunstation = hwDataOverrunstationHistoryMapper.selectById(fId);

			if (hwDataOverrunstation == null) {
				return ResponeResultUtil.fail(HWSystemCode.OVERRUNDATAID_NOT_EXIT);
			}

			hwDataOverrunstation.setjZz(jZz);
			hwDataOverrunstation.setjSfcz(jSfcz);

			if (hwDataOverrunstation.getjCxds() == null) {
				hwDataOverrunstation.setjCxds(jCxds);
			} else {
				// 超限吨数
				int a = jCxds.compareTo(hwDataOverrunstation.getjCxds());

				if (a == 1) {
					hwDataOverrunstation.setjCxds(jCxds);
				}
			}
			hwDataOverrunstation.setjCzy(jCzy);
			hwDataOverrunstation.setjJcsj(new Timestamp(System.currentTimeMillis()));
			hwDataOverrunstation.setjCreatetime(new Timestamp(System.currentTimeMillis()));
			hwDataOverrunstation.setjCtpic(jCtpic);
			hwDataOverrunstation.setjCspic(jCspic);
			hwDataOverrunstation.setjCwpic(jCwpic);
			hwDataOverrunstation.setjCppic(jCppic);

			hwDataOverrunstation.setfAreacode(fAreacode);
			hwDataOverrunstation.setfOverruncode(fOverruncode);
			hwDataOverrunstation.setfOverrunname(fOverrunname);

			Integer update = hwDataOverrunstationHistoryMapper.update(hwDataOverrunstation, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
			return ResponeResultUtil.success(HWSystemCode.SUCCESS);
		} else {

			// 动态称重数据
			hwDataOverrunstationVo.setfId(RandomNumberUtil.getNumber());
			hwDataOverrunstationVo.setdCpys(dCpys);
			hwDataOverrunstationVo.setdZs(dZs);
			hwDataOverrunstationVo.setdZz(dZz);
			hwDataOverrunstationVo.setdXz(dXz);
			hwDataOverrunstationVo.setdCx(dCx);
			hwDataOverrunstationVo.setdCxl(dCxl);
			hwDataOverrunstationVo.setdJcsj(new Timestamp(System.currentTimeMillis()));
			hwDataOverrunstationVo.setdCs(dCs);
			hwDataOverrunstationVo.setdCd(dCd);
			hwDataOverrunstationVo.setdFx(dFx);
			hwDataOverrunstationVo.setdSfcx(dSfcx);
			hwDataOverrunstationVo.setdCptx(dCptx);
			hwDataOverrunstationVo.setdCttx(dCttx);
			hwDataOverrunstationVo.setdCreatetime(new Timestamp(System.currentTimeMillis()));

			hwDataOverrunstationVo.setfAreacode(fAreacode);
			hwDataOverrunstationVo.setfOverruncode(fOverruncode);
			hwDataOverrunstationVo.setfOverrunname(fOverrunname);

			Integer insert = hwDataOverrunstationHistoryMapper.insert(hwDataOverrunstationVo);
			if (insert != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
			return ResponeResultUtil.success(HWSystemCode.SUCCESS);
		}

	}

	// 督导
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class,
			HWSystemException.class, })
	public Map<String, Object> superVision(DataOverrunstationHistoryVo vo) {
		// 查询下一步的工作流code
		HwBaseWorkflow hwBaseWorkflow = baseWorkflowService.getStartBaseWorkflow("JCDD");
		// 创建督导信息
		String spvId = RandomNumberUtil.getNumber();
		HwSupervision hwSupervision = new HwSupervision();
		hwSupervision.setfSpvid(spvId);
		hwSupervision.setfFromobjectid(vo.getfId());
		hwSupervision.setfFromtype(vo.getfOverrunname());
		hwSupervision.setfFromdepcode(vo.getfTypecode());
		hwSupervision.setfFromuserid(vo.getfUserAccount());
		hwSupervision.setfFromusername(vo.getfFromusername());
		hwSupervision.setfFromtime(new Timestamp(System.currentTimeMillis()));
		hwSupervision.setfTitle(vo.getfTitle());
		hwSupervision.setfFromsrc(5);
		hwSupervision.setfSpvcontent(vo.getfSpvcontent());
		hwSupervision.setfCurrentstep(hwBaseWorkflow.getWorkflowNextcode());
		hwSupervision.setfBacktype(3);
		hwSupervision.setfBackdepcode(vo.getfBackdepcode());
		hwSupervision.setfBackdepname(vo.getfBackdepname());
		hwSupervision.setfBackassignstate(0);
		hwSupervision.setIsStagestate(0);
		Integer flag = hwSupervisionMapper.insert(hwSupervision);
		if (flag != 1) {
			throw new HWSystemException();
		}

		// 创建各阶段审批操作过程
		HwCommandWorkflow hwCommandWorkflow = new HwCommandWorkflow();
		hwCommandWorkflow.setCommandworkflowId(RandomNumberUtil.getNumber());
		hwCommandWorkflow.setWorkflowModule(hwBaseWorkflow.getWorkflowModule());
		hwCommandWorkflow.setWorkflowType(hwBaseWorkflow.getWorkflowType());
		hwCommandWorkflow.setWorkflowCode(hwBaseWorkflow.getWorkflowCode());
		hwCommandWorkflow.setWorkflowCodename(hwBaseWorkflow.getWorkflowCodename());
		hwCommandWorkflow.setWorkflowNextcode(hwBaseWorkflow.getWorkflowNextcode());
		hwCommandWorkflow.setWorkflowNextcodename(hwBaseWorkflow.getWorkflowNextcodename());
		hwCommandWorkflow.setCommandworkflowButtonname(vo.getButtonName());
		hwCommandWorkflow.setfAccount(vo.getfUserAccount());
		hwCommandWorkflow.setfFromdepcode(vo.getfTypecode());
		hwCommandWorkflow.setfFromdepname(vo.getfOverrunname());
		hwCommandWorkflow.setCommandworkflowDatatime(new Date());
		hwCommandWorkflow.setCommandworkflowContent(vo.getfSpvcontent());
		hwCommandWorkflow.setObjectId(spvId);
		// 向各阶段审批操作过程表插入数据
		boolean success = commandWorkflowService.addWorkflow(hwCommandWorkflow);
		if (!success) {
			throw new HWSystemException();
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

}
